﻿using System;
using System.Data;
using System.Web.UI.WebControls;

namespace WebBill
{
	public partial class Info : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string connString = DO.Common.DreamOnConfig.connectionString_web;
			//using (SqlConnection conn = new SqlConnection(connString))
			//{
			//    SqlCommand cmd = new SqlCommand();
			//    cmd.Connection = conn;
			//    DataSet ds = new DataSet();
			//    SqlDataAdapter adp = new SqlDataAdapter(cmd);

			//    //////**************************************************//

			//    cmd.CommandText = "SELECT ID, NAME, TOTAL_COST, ROUND(TOTAL_UNIT, 3) AS TOTAL_UNIT, ASSET_VALUE, ROUND(ROI, 4) AS ROI,AVG_COST FROM View_CUSTOMERS";
			//    adp.Fill(ds, "Cust");

			//    setOiRatioPie(ds.Tables["Cust"], oiRatio);
			//    setCostBarChart(ds.Tables["Cust"], bar_totalCost);
			//    setAvgCostBarChart(ds.Tables["Cust"], bar_avgCost);
			//    ds.Dispose();
			//}
			DO.Common.Trace.SetLogFile(DO.Common.DreamOnConfig.logFolder, "Bill");

			try
			{
				using (DO.Common.DataFunction DF = new DO.Common.DataFunction(connString))
				{
					DataSet ds = new DataSet();
					string SQLstr;
					SQLstr = "SELECT ID, NAME, TOTAL_COST, ROUND(TOTAL_UNIT, 3) AS TOTAL_UNIT, ASSET_VALUE, ROUND(ROI, 4) AS ROI,AVG_COST FROM View_CUSTOMERS";

					DF.RunSqlCommand(SQLstr, out ds);

					setOiRatioPie(ds.Tables[0], oiRatio);
					setCostBarChart(ds.Tables[0], bar_totalCost);
					setAvgCostBarChart(ds.Tables[0], bar_avgCost);
				}
			}
			catch (Exception ex)
			{
				DO.Common.Trace.ErrorWrite("Datafunction", "Description:" + e.ToString() + "\r\n");
			}
		}

		private void setAvgCostBarChart(DataTable dt, Image iim)
		{
			/**/
			//-------常數區--------
			const string width = "500";
			const string height = "210";
			/**/
			//---------------------
			//https://chart.googleapis.com/chart?cht=bvg&chs=540x200&chd=t:50000,30000,30000,410000|-5000,3000,1000,10000|-10,10,3,3&chbh=8,4,20&chco=4D89F9,C6D9FD,FF9900&chxt=y,r,r&chds=-205000,410000,-7500,15000,-10,20&chxr=0,-205000,410000|1,-7500,15000|2,-10,20
			string gchart = "cht=bvg";	//圓餅圖
			gchart += "&chs=" + width + "x" + height;	//圖表大小
			gchart += "&chbh=10,3,20";
			gchart += "&chco=4D89F9,C6D9FD,FF9900";
			//gchart += "&chxt=y,r,r,x";
			gchart += "&chxt=x";
			gchart += "&chdl=平均成本(仟)|損益(仟)|報酬率";

			//string chd = "&chd=t:";
			//string chdl = "&chdl=";
			//string chl = "&chl=";
			float cost_max = 0f; float cost_min = 0f;
			float profit_max = 0f; float profit_min = 0f;
			float roi_max = 0f; float roi_min = 0f;

			string[] chd = new string[3];
			gchart += "&chxl=0:";
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				gchart += "|" + dt.Rows[i]["NAME"].ToString();
				//float cost = float.Parse(dt.Rows[i]["TOTAL_COST"].ToString());
				float cost = Convert.ToSingle(Math.Round(float.Parse(dt.Rows[i]["AVG_COST"].ToString()) / 1000, 3));
				float totalcost = Convert.ToSingle(Math.Round(float.Parse(dt.Rows[i]["TOTAL_COST"].ToString()) / 1000, 3));
				//float profit = float.Parse(dt.Rows[i]["ASSET_VALUE"].ToString()) - cost;
				float profit = Convert.ToSingle(Math.Round(float.Parse(dt.Rows[i]["ASSET_VALUE"].ToString()) / 1000, 3)) - totalcost;
				float roi = float.Parse(dt.Rows[i]["ROI"].ToString()) * 100;

				cost_max = Math.Max(cost_max, cost);
				cost_min = Math.Min(cost_min, cost);

				profit_max = Math.Max(profit_max, profit);
				profit_min = Math.Min(profit_min, profit);

				roi_max = Math.Max(roi_max, roi);
				roi_min = Math.Min(roi_min, roi);

				chd[0] = chd[0] + cost.ToString();
				chd[1] = chd[1] + profit.ToString();
				chd[2] = chd[2] + roi.ToString();

				if (i != dt.Rows.Count - 1)
				{
					for (int j = 0; j < chd.Length; j++) { chd[j] = chd[j] + ","; }
				}
			}
			gchart += "&chd=t:" + chd[0] + "|" + chd[1] + "|" + chd[2] + "";


			float cost_10 = Math.Abs(cost_max) + Math.Abs(cost_min) / 10;
			float profit_10 = Math.Abs(profit_max) + Math.Abs(profit_min) / 10;
			float roi_10 = Math.Abs(roi_max) + Math.Abs(roi_min) / 10;

			float goba_max = Math.Max(roi_max / roi_10, Math.Max(cost_max / cost_10, profit_max / profit_10));
			float goba_min = Math.Min(roi_min / roi_10, Math.Min(cost_min / cost_10, profit_min / profit_10));

			gchart += "&chds=" + cost_10 * goba_min + "," + cost_10 * goba_max + "," + profit_10 * goba_min + "," + profit_10 * goba_max + "," + roi_10 * goba_min + "," + roi_10 * goba_max + "";
			gchart += "&chxr=0," + cost_10 * goba_min + "," + cost_10 * goba_max + "|1," + profit_10 * goba_min + "," + profit_10 * goba_max + "|2," + roi_10 * goba_min + "," + roi_10 * goba_max + "";
			gchart += "&chm=N,000000,0,,10|N,000000,1,,10|N,000000,2,,10";
			//gchart += "&chm=N,000000,1,-10,10";
			//gchart += "&chm=N,000000,2,20,10";
			//<img src="https://chart.googleapis.com/chart?cht=bvg&amp;chs=100x75&amp;chd=t:5,5,5|10,10,10|15,15,15&amp;chds=0,120&amp;chco=4d89f9,c6d9fd,00B88A&amp;chbh=20&amp;chds=0,20&amp;chbh=a&amp;chxs=0,000000,0,0,_&amp;chxt=y&amp;chm=N,000000,0,,10|N,000000,1,,10|N,000000,2,,10" alt="Grouped chart">

			iim.ImageUrl = "https://chart.googleapis.com/chart?" + gchart;
		}


		private void setCostBarChart(DataTable dt, Image iim)
		{
			/**/
			//-------常數區--------
			const string width = "500";
			const string height = "210";
			/**/
			//---------------------
			//https://chart.googleapis.com/chart?cht=bvg&chs=540x200&chd=t:50000,30000,30000,410000|-5000,3000,1000,10000|-10,10,3,3&chbh=8,4,20&chco=4D89F9,C6D9FD,FF9900&chxt=y,r,r&chds=-205000,410000,-7500,15000,-10,20&chxr=0,-205000,410000|1,-7500,15000|2,-10,20
			string gchart = "cht=bvg";	//圓餅圖
			gchart += "&chs=" + width + "x" + height;	//圖表大小
			gchart += "&chbh=10,3,20";
			gchart += "&chco=4D89F9,C6D9FD,FF9900";
			//gchart += "&chxt=y,r,r,x";
			gchart += "&chxt=x";
			gchart += "&chdl=投入金額(仟)|損益(仟)|報酬率";

			//string chd = "&chd=t:";
			//string chdl = "&chdl=";
			//string chl = "&chl=";
			float cost_max = 0f; float cost_min = 0f;
			float profit_max = 0f; float profit_min = 0f;
			float roi_max = 0f; float roi_min = 0f;

			string[] chd = new string[3];
			gchart += "&chxl=0:";
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				gchart += "|" + dt.Rows[i]["NAME"].ToString();
				//float cost = float.Parse(dt.Rows[i]["TOTAL_COST"].ToString());
				float cost = Convert.ToSingle(Math.Round(float.Parse(dt.Rows[i]["TOTAL_COST"].ToString()) / 1000, 3));
				//float profit = float.Parse(dt.Rows[i]["ASSET_VALUE"].ToString()) - cost;
				float profit = Convert.ToSingle(Math.Round(float.Parse(dt.Rows[i]["ASSET_VALUE"].ToString()) / 1000, 3)) - cost;
				float roi = float.Parse(dt.Rows[i]["ROI"].ToString()) * 100;

				cost_max = Math.Max(cost_max, cost);
				cost_min = Math.Min(cost_min, cost);

				profit_max = Math.Max(profit_max, profit);
				profit_min = Math.Min(profit_min, profit);

				roi_max = Math.Max(roi_max, roi);
				roi_min = Math.Min(roi_min, roi);

				chd[0] = chd[0] + cost.ToString();
				chd[1] = chd[1] + profit.ToString();
				chd[2] = chd[2] + roi.ToString();

				if (i != dt.Rows.Count - 1)
				{
					for (int j = 0; j < chd.Length; j++) { chd[j] = chd[j] + ","; }
				}
			}
			gchart += "&chd=t:" + chd[0] + "|" + chd[1] + "|" + chd[2] + "";


			float cost_10 = Math.Abs(cost_max) + Math.Abs(cost_min) / 10;
			float profit_10 = Math.Abs(profit_max) + Math.Abs(profit_min) / 10;
			float roi_10 = Math.Abs(roi_max) + Math.Abs(roi_min) / 10;

			float goba_max = Math.Max(roi_max / roi_10, Math.Max(cost_max / cost_10, profit_max / profit_10));
			float goba_min = Math.Min(roi_min / roi_10, Math.Min(cost_min / cost_10, profit_min / profit_10));

			gchart += "&chds=" + cost_10 * goba_min + "," + cost_10 * goba_max + "," + profit_10 * goba_min + "," + profit_10 * goba_max + "," + roi_10 * goba_min + "," + roi_10 * goba_max + "";
			gchart += "&chxr=0," + cost_10 * goba_min + "," + cost_10 * goba_max + "|1," + profit_10 * goba_min + "," + profit_10 * goba_max + "|2," + roi_10 * goba_min + "," + roi_10 * goba_max + "";
			gchart += "&chm=N,000000,0,,10|N,000000,1,,10|N,000000,2,,10";
			//gchart += "&chm=N,000000,1,-10,10";
			//gchart += "&chm=N,000000,2,20,10";
			//<img src="https://chart.googleapis.com/chart?cht=bvg&amp;chs=100x75&amp;chd=t:5,5,5|10,10,10|15,15,15&amp;chds=0,120&amp;chco=4d89f9,c6d9fd,00B88A&amp;chbh=20&amp;chds=0,20&amp;chbh=a&amp;chxs=0,000000,0,0,_&amp;chxt=y&amp;chm=N,000000,0,,10|N,000000,1,,10|N,000000,2,,10" alt="Grouped chart">

			iim.ImageUrl = "https://chart.googleapis.com/chart?" + gchart;
		}

		private void setOiRatioPie(DataTable dt, Image iim)
		{
			/**/
			//-------常數區--------
			const string width = "500";
			const string height = "210";
			/**/
			//---------------------
			string gchart = "cht=p";	//圓餅圖
			gchart += "&chs=" + width + "x" + height;	//圖表大小
			//gchart += "&chco=81FF6D";

			string chd = "&chd=t:";
			string chdl = "&chdl=";
			string chl = "&chl=";
			float total = 0;
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				total += float.Parse(dt.Rows[i]["TOTAL_UNIT"].ToString());
			}
			for (int i = 0; i < dt.Rows.Count; i++)
			{

				float j = (float)Math.Round(float.Parse(dt.Rows[i]["TOTAL_UNIT"].ToString()) / total, 3);

				chd += Math.Round(float.Parse(dt.Rows[i]["TOTAL_UNIT"].ToString())).ToString();
				chdl += dt.Rows[i]["NAME"].ToString() + "," + float.Parse(dt.Rows[i]["TOTAL_UNIT"].ToString()).ToString();// +"," + j * 100 + "%";
				chl += dt.Rows[i]["NAME"].ToString() + "," + j * 100 + "%";
				if (i != dt.Rows.Count - 1)
				{
					chd += ",";
					chdl += "|";
					chl += "|";
				}
			}

			gchart += chd + chdl + chl;

			iim.ImageUrl = "https://chart.googleapis.com/chart?" + gchart;
		}
	}
}